import 'dart:convert';

import 'package:test_flutter/Config/Config.dart';
import 'package:test_flutter/utils/Md5Util.dart';

class FieldsUtil {
  static Map BaseMap() {
    Map<String, String> map = new Map<String, String>();
    map['v'] = Config.v.toString();
    map['sid'] = Config.sid.toString();
    map['d'] = Config.d.toString();
    map['shop'] = Config.shop.toString();
    map['term'] = Config.term.toString();
    map['oper'] = Config.oper.toString();
    map['format'] = Config.format.toString();
    return map;
  }

  static Map getRequestMap(Map<String, String> requestMap) {
    Map<String, String> map = BaseMap();
    map.addAll(requestMap);
    map['sig'] = getSign(requestMap).toString();

    print("-----cacaca----"+json.encode(map));


    return map;
  }

  static String getSign(Map<String, String> requestMap) {
    Map<String, String> map = BaseMap();
    map.addAll(requestMap);
    var strs = new List();
    String key = Config.key; //
    String sign = "";
    map.forEach((k, v) {
      print('$k=$v');
      strs.add('$k=$v');
    });
    strs.sort();
    String str = "";
    for(int i=0; i < map.length; i++){
      str += strs[i];
    }
    // 追加key
    str += key;
    print("追加结果： "+str);
    // 计算MD5签名
    sign = Md5Util.MD5(str);
    print('sign ='+sign);
    return sign;
  }
}
